import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

// import 'package:xingsu/util/user_preferences.dart';
import '../util/use_preferences.dart';
import 'colors.dart';
import 'package:get/get.dart';

class ThemeUtil {
  static ThemeData getTheme({bool isDarkMode = false}) {
    return ThemeData(
        brightness: isDarkMode ? Brightness.dark : Brightness.light,
        primaryColor: Colours.app_main,
        backgroundColor:
            isDarkMode ? Colours.dark_material_bg : Colours.bg_gray,
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        scaffoldBackgroundColor:
            isDarkMode ? Colours.dark_bgColor : Colours.bgColor,
        dialogBackgroundColor:
            isDarkMode ? Colours.dark_bgColor : Colours.bgColor,
        shadowColor: isDarkMode ? Colors.white12 : Colors.black12,
        dividerTheme: DividerThemeData(
          color: isDarkMode ? Colours.dark_line : Colours.line,
        ),
        iconTheme:
            IconThemeData(color: isDarkMode ? Colors.white : Colors.black45),
        appBarTheme: AppBarTheme(
            elevation: 0,
            centerTitle: true,
            titleTextStyle: TextStyle(
                color: isDarkMode ? Colors.white : Colors.black,
                fontSize: 18,
                fontWeight: FontWeight.w700),
            iconTheme:
                IconThemeData(color: isDarkMode ? Colors.white : Colors.black),
            color: isDarkMode ? Colours.dark_bgColor : Colours.bgColor,
            systemOverlayStyle: isDarkMode
                ? SystemUiOverlayStyle.light
                : SystemUiOverlayStyle.dark),
        tabBarTheme: const TabBarTheme(
            unselectedLabelColor: Colors.grey, labelColor: Color(0xFFE33B62)));
  }

  static ThemeMode getThemeMode() {
    final int theme = UserPreferences.getThemeMode();
    switch (theme) {
      case 2:
        return ThemeMode.dark;
      case 1:
        return ThemeMode.light;
      default:
        return ThemeMode.system;
    }
  }

  static void setThemeMode(int value) {
    UserPreferences.setThemeMode(value);
    if (value == 0) {
      Get.changeThemeMode(ThemeMode.system);
    } else if (value == 1) {
      Get.changeThemeMode(ThemeMode.light);
    } else {
      Get.changeThemeMode(ThemeMode.dark);
    }
  }

  static bool isDark(BuildContext context) {
    return Theme.of(context).brightness == Brightness.dark;
  }
}

extension ThemeExtension on BuildContext {
  bool get isDark => ThemeUtil.isDark(this);

  Color get backgroundColor => Theme.of(this).scaffoldBackgroundColor;

  Color get dialogBackgroundColor => Theme.of(this).canvasColor;
}
